package com.example.demo.nios;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.util.Iterator;
import java.util.Set;

public class SelectSocket   {
    public static void select() throws IOException {
        Selector selector = Selector.open();
        ServerSocketChannel channel = ServerSocketChannel.open();
        channel.bind(new InetSocketAddress(2000));
        channel.configureBlocking(true);
        channel.accept();
        channel.register(selector, SelectionKey.OP_ACCEPT|SelectionKey.OP_CONNECT|SelectionKey.OP_READ|SelectionKey.OP_WRITE);
        while (true){
            int select = selector.select();
            if(select == 0){
                continue;
            }
            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            while (iterator.hasNext()){
                SelectionKey selectionKey = iterator.next();
                SelectableChannel chan = selectionKey.channel();
                if(selectionKey.isAcceptable()){

                }
                else if(selectionKey.isConnectable()){

                }
                else if(selectionKey.isReadable()){

                }
                else if(selectionKey.isWritable()){

                }

            }
        }

    }

    public static void main(String[] args) throws IOException {
        select();
    }
}
